package com.xxl.sso.server.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.icom.sso.util.RetKit;
import com.xxl.sso.core.entity.PageData;
import com.xxl.sso.server.core.model.Project;
import com.xxl.sso.server.core.model.SysUser;
import com.xxl.sso.server.service.ProjectService;

@Controller
@RequestMapping("/project")
public class ProjectController {
	private static Logger logger = Logger.getLogger(UserController.class);

	@Value("${upload_path}")
	private String uploadPath;

	@Autowired
	ProjectService projectService;

	@RequestMapping("/list")
	public String seeProjects(Model model) {
		List<Project> projects = projectService.findProjects();

		for (Project project : projects) {
			String path = project.getLogoPath();
			path = path.replaceAll("\\\\", "/");
			project.setLogoPath(path);
		}

		model.addAttribute("projects", projects);
		return "project/project_list";
	}

	@RequestMapping("/manageProject")
	@ResponseBody
	public PageData<Project> findProject(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer rows) {
		PageData<Project> result = projectService.findPageProject(page, rows);
		return result;
	}

	@RequestMapping("/addProject")
	public String addProject() {
		return "project/project_add";
	}

	@RequestMapping("/saveProject")
	@ResponseBody
	public RetKit<String> saveProject(@RequestParam(value = "file", required = false) MultipartFile file,
			Project project) {
		logger.info("新增项目.......");
		RetKit<String> ret = new RetKit<String>(null);

		if (project.getName() == null || project.getName().trim().length() == 0) {
			ret.setFail();
			ret.setMsg("名称不能为空！");
			return ret;
		} else if (project.getAddress() == null || project.getAddress().trim().length() == 0) {
			ret.setFail();
			ret.setMsg("项目地址不能为空！");
			return ret;
		}

		logger.info("新增项目名称......." + project.getName());
		RetKit<String> result = projectService.uploadFile(file, uploadPath);

		if (result.getFlag()) {
			project.setLogoPath(uploadPath + File.separator + result.getData());
		}
		ret = projectService.saveProject(project);

		return ret;
	}

	@RequestMapping("/changeStatus")
	@ResponseBody
	public RetKit<String> changeStatus(HttpServletRequest request, HttpServletResponse response) {
		String projectId = request.getParameter("project_id");
		String status = request.getParameter("status");

		RetKit<String> retKit = projectService.changeStatus(projectId, status);

		return retKit;
	}

	@RequestMapping("/editInfo")
	public String changeProjectInfo(Model model, HttpServletRequest request, HttpServletResponse response) {
		String projectId = request.getParameter("project_id");

		RetKit<Project> retKit = projectService.changeProjectInfo(projectId);
		model.addAttribute("project", (Project) retKit.getData());

		return "project/project_edit";
	}

	/**
	 * 修改项目信息
	 * @param project
	 * @return
	 */
	@RequestMapping("/saveChangeInfo")
	@ResponseBody
	public RetKit<String> saveChangeInfo(Project project) {
		RetKit<String> ret = new RetKit<String>(null);

		if (project.getName() == null || project.getName().trim().length() == 0) {
			ret.setFail();
			ret.setMsg("名称不能为空！");
			return ret;
		} else if (project.getAddress() == null || project.getAddress().trim().length() == 0) {
			ret.setFail();
			ret.setMsg("项目地址不能为空！");
			return ret;
		}
		logger.info("修改项目信息.......项目名称：" + project.getName());
		ret = projectService.saveChangeInfo(project);

		return ret;
	}
	
	/**
	 * 删除项目信息---物理删除
	 * @param project_id
	 * @return
	 */
	@RequestMapping("/delProjecct")
	@ResponseBody
	public RetKit<String> delProject(HttpServletRequest request, HttpServletResponse response) {
		RetKit<String> ret = new RetKit<String>(null);
		String  projectId = request.getParameter("project_id");
		Project project =  projectService.findProjectById(projectId);
		
		logger.info("删除项目---"+project.getName());
		
		//记录删除日志，包含操作人、删除项目信息
		ret= projectService.delProject(projectId);
		
		if (ret.getFlag()) {
			logger.info("删除项目---"+project.getName()+";成功！");
		}else {
			logger.info("删除项目---"+project.getName()+";失败！");
		}
		
		return ret;
	}

}
